/**   
 * @Title: AbstarcAvroClient.java 
 * @Package com.org.framework.net.avro 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author cssuger@163.com   
 * @date 2017年3月7日 下午1:40:24 
 * @version V1.0   
 */
package com.bicon.base.data.common.net.avro;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.avro.Protocol;
import org.apache.avro.generic.GenericRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bicon.base.data.common.net.Client;


/** 
 *@ClassName: AbstarcAvroClient 
 *@Description: TODO(这里用一句话描述这个类的作用) 
 *@author cssuger@163.com 
 *@date 2017年3月7日 下午1:40:24  
 */
public abstract class AbstractAvroClient implements Client{

	private Logger logger = LoggerFactory.getLogger(AbstractAvroClient.class);
	
	private String host;
	
	private int port;
	
	public AbstractAvroClient(String host,int port){
		this.host =host;
		this.port = port;
	}
	
	/**
	 * 
	 * @Title: builderMessage 
	 * @Description: 构建消息 
	 * @param @return    设定文件 
	 * @return GenericRecord    返回类型 
	 * @throws
	 */
	public abstract GenericRecord builderMessage(Protocol protocol);
	
	/**
	 * 
	 * @Title: request 
	 * @Description: 发送消息 
	 * @param @param methodName
	 * @param @param prarms    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	public void request(String methodName,String prarms){
//		AvroUtil util = new AvroUtil();
//		InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(Constants.AVRO_PATH);
//		Protocol protocol = util.parseProtocol();
//		//构建消息
//		GenericRecord requestData = this.builderMessage(protocol);
//		//构建方法请求协议
//		GenericRecord  request = new  GenericData.Record(protocol.getMessages().get(methodName).getRequest());
//		request.put(prarms, requestData);
//		Transceiver transceiver = this.connection();
//		GenericRequestor requestor = null;
//		try {
//		  requestor = new GenericRequestor(protocol, transceiver);
//		  requestor.request(methodName, request, new Callback<Record>(){
//
//			@Override
//			public void handleResult(Record result) {
//				logger.info("发送成功");
//				
//			}
//
//			@Override
//			public void handleError(Throwable error) {
//				error.printStackTrace();
//				logger.info("发送失败");
//				
//			}
//			  
//		  });
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally{
//			close(transceiver);
//		}
		//发送消息
	}
	
//	private void close(Transceiver transceiver){
//		try{
//			if(null != transceiver){
//				transceiver.close();
//			}
//		}catch(Exception ex){
//			
//		}
//		
//	}
	
	private static class TransceiverThreadFactory implements ThreadFactory {
	    private final AtomicInteger threadId = new AtomicInteger(0);
	    private final String prefix;

	    /**
	     * Creates a TransceiverThreadFactory that creates threads with the
	     * specified name.
	     * @param prefix the name prefix to use for all threads created by this
	     * ThreadFactory.  A unique ID will be appended to this prefix to form the
	     * final thread name.
	     */
	    public TransceiverThreadFactory(String prefix) {
	      this.prefix = prefix;
	    }

	    @Override
	    public Thread newThread(Runnable r) {
	      Thread thread = new Thread(r);
	      thread.setDaemon(true);
	      thread.setName(prefix + " " + threadId.incrementAndGet());
	      return thread;
	    }
	  }

//	@Override
//	public Transceiver connection() {
//		  ExecutorService bossExecutor = Executors.newCachedThreadPool(new TransceiverThreadFactory(
//			          "Avro " + NettyTransceiver.class.getSimpleName() + " Boss"));
//		  ExecutorService workerExecutor = Executors.newCachedThreadPool(new TransceiverThreadFactory(
//			          "Avro " + NettyTransceiver.class.getSimpleName() + " I/O Worker"));
//		  NioClientSocketChannelFactory socketChannelFactory = new NioClientSocketChannelFactory(
//	              bossExecutor, workerExecutor, Runtime.getRuntime().availableProcessors()*2);
//		Transceiver t = null;
//		try {
//			t = new NettyTransceiver(new InetSocketAddress(this.host, this.port),socketChannelFactory, 2000L);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return t;
//
//	}

}
